package com.zhijiaoyun.common;

import com.alibaba.fastjson.JSONObject;
import com.shisfish.util.JwtUtils;
import com.zhijiaoyun.model.Admin;
import com.zhijiaoyun.model.Teacher;
import com.zhijiaoyun.model.User;
import io.jsonwebtoken.Claims;

import java.util.Date;

/**
 * @author shisfish
 * @date 2017/12/14
 */
public class ValidationUtils {
	
	public static final long DAY_ONE = 24L * 60 * 60 * 1000;
	public static final long DAY_MONTH = 30L * 24 * 60 * 60 * 1000;

    /**
     * 生成subject信息
     * @param teacher
     * @return
     */
    public static String generalSubject(Teacher teacher){
        JSONObject jo = new JSONObject();
        jo.put("teacherId", teacher.getId());
        jo.put("name", teacher.getName());
        return jo.toJSONString();
    }

    public static String generalSubject(User user){
        JSONObject jo = new JSONObject();
        jo.put("userId", user.getUserId());
        jo.put("email", user.getEmail());
        return jo.toJSONString();
    }

    public static String generalSubject(Admin admin){
        JSONObject jo = new JSONObject();
        jo.put("adminId", admin.getAdminId());
        jo.put("account", admin.getAccount());
        return jo.toJSONString();
    }

    public static String createJWT(Teacher teacher, Long expDate) {
        String subject = generalSubject(teacher);
        String token = "";
        try {
            token = JwtUtils.createJWT("1", subject, expDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

    public static String createJWT(User user, Long expDate) {
        String subject = generalSubject(user);
        String token = "";
        try {
            token = JwtUtils.createJWT("1", subject, expDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

    public static String createJWT(Admin admin, Long expDate) {
        String subject = generalSubject(admin);
        String token = "";
        try {
            token = JwtUtils.createJWT("1", subject, expDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

    public static Long getIdByToken(String token, String idType) {
        try {
            Claims claims = JwtUtils.parseJWT(token);
            Object exp = claims.get("exp");
            Long longs = StringUtils.parseLong(exp.toString(), 0) * 1000;
            Date expDate = new Date(longs);
            if (expDate.before(new Date())) {
                return 0L;
            }
            String sub = claims.getSubject();
            JSONObject jsonObject = JSONObject.parseObject(sub);
            Long userId = jsonObject.getLong(idType);
            if (userId == null) {
                return 0L;
            }
            return userId;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    public static Long getUserIdByToken(String token) {
        return getIdByToken(token, "userId");
    }

    public static Long getAdminIdByToken(String token) {
        return getIdByToken(token, "adminId");
    }

}
